﻿var form, $;
layui.use(['element', 'layer', 'form'], function() {
	form = layui.form;
	$ = layui.jquery;

	//自定义验证
	form.verify({
		passWord: [/^[\S]{1,12}$/, '密码必须1到12位'],
		account: function(value) {
			if(value.length <= 0 || value.length > 10) {
				return "账号必须1到10位"
			}
			var reg = /^[a-zA-Z0-9]*$/;
			if(!reg.test(value)) {
				return "账号只能为英文或数字";
			}
		},
		luotest_response: function(value) {
			if(value.length < 1) {
				return '请点击人机识别验证';
			}
		},
	});
	//监听登录提交
	form.on('submit(login)', function(data) {
		  	if(data.field.luotest_response==null||data.field.luotest_response.length<=0){
		  		layer.msg('请点击人机识别验证');
		  		return false;
		  	}
		Login(data.field.account, data.field.passWord);
		return false;
	});

	//检测键盘按下
	$('body').keydown(function(e) {
		if(e.keyCode == 13) { //Enter键           
			$('button[lay-filter=login]').click();
		}
	});

});
//登录
function Login(Account, Password) {
	var index = layer.load(0);
	var data1 = {
		account: Account,
		password: Password
	}
	$.ajax({
		url: apiUrl + 'api/Admin/Login',
		type: 'post',
		async: true,
		data: JSON.stringify(data1),
		contentType: 'application/json',
		dataType: 'json',
		success: function(jsondata) {
			if(jsondata.Type == 1) {
				sessionStorage.setItem('id', jsondata.Data.Id);
				sessionStorage.setItem('account', jsondata.Data.Account);
				sessionStorage.setItem('token', jsondata.Data.Token);
				location.href = "index.html";

			} else {
				layer.msg("用户名或密码不正确");
			}
		},
		complete: function(data, xhr) {
			layer.close(index);
		}

	});

}